﻿//ini配置文件存取类
//2011-6-3
//需要事先写入全部数据

#pragma once
#include <fstream>
#include <map>
//#include "windows.h"
//#include "const.h"

#define NOVALUE L"novalue"
class inisetter
{
public:

	
	//设置默认值，会检查配置存在，不存在会自动创建
	inisetter()
	{
	}

	inisetter(wchar_t*path_)
	{
		wcscpy(path,path_);
		if(!wxFile::Exists(path))
		{
			char tmpc[256];		
			wcstombs(tmpc,path_,256);		
			std::ofstream fout( tmpc );	
			if ( fout )		
				fout.close();	
			else			
				MessageBox(0,L"创建配置文件失败",L"Warning",0);
		}
	}
	virtual ~inisetter(void)
	{
	}
	void setpath(const wchar_t *p)
	{
		wcscpy(path,p);
		if(!wxFile::Exists(path))
		{
			char tmpc[256];		
			wcstombs(tmpc,p,256);	
			std::ofstream fout( tmpc );
			if ( fout )  // 如果创建成功
				fout.close(); 
			else
				MessageBox(0,L"创建配置文件失败",L"Warning",0);
		}
	}
	//会逐一对照将空缺的补全
	void setdefault(const wchar_t * app,std::map< const wchar_t *,const  wchar_t *>& m)
	{
		std::map<const wchar_t*,const wchar_t*>::iterator i;
		wchar_t tmp[100];
		for(i=m.begin();i!=m.end();i++)
		{
			get(app,i->first,tmp);
			if(wcscmp(tmp,NOVALUE)==0 )
				set(app,i->first,i->second);
		}
	}

	void set(const wchar_t*appname,const wchar_t* key,const wchar_t*value)
	{
		WritePrivateProfileStringW(appname,key,value,path);
	}
	void set(const wchar_t*appname,const wchar_t* key,int value)
	{
		wchar_t s[100];
		wsprintf(s,L"%d",value);
		WritePrivateProfileStringW(appname,key,s,path);
	}
	void get(const wchar_t*appname,const wchar_t* key,wchar_t*value)
	{
		GetPrivateProfileStringW(appname,key,NOVALUE,value,50,path);
	}

private:
	wchar_t path[256];
};

